package com.gewu.ordlelist;


import android.view.MenuItem;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.gewu.ordlelist.base.BaseActivity;
import com.gewu.ordlelist.fragment.fourFragment;
import com.gewu.ordlelist.fragment.oneFragment;
import com.gewu.ordlelist.fragment.thrFragment;
import com.gewu.ordlelist.fragment.twoFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.List;

public class MainActivity extends BaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    private com.google.android.material.bottomnavigation.BottomNavigationView mainNavigation;
    private oneFragment oneFragment = new oneFragment();
    private twoFragment twoFragment = new twoFragment();
    private thrFragment thrFragment = new thrFragment();
    private fourFragment fourFragment = new fourFragment();

    @Override
    public int FindLayout() {
        return R.layout.activity_main;
    }

    @Override
    public Toolbar getToolBar() {
        return null;
    }

    @Override
    public boolean isShowBackToolbar() {
        return false;
    }

    @Override
    public String setToolbarText() {
        return null;
    }

    @Override
    public void initview() {
        mainNavigation = (BottomNavigationView) findViewById(R.id.mainNavigation);
        mainNavigation.setOnNavigationItemSelectedListener(this);
        initFragment();
    }

    /**
     * 初始化fragment
     */
    private void initFragment() {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.content, oneFragment).add(R.id.content, twoFragment).add(R.id.content, thrFragment)
                .add(R.id.content, fourFragment).hide(twoFragment).hide(thrFragment).hide(fourFragment).show(oneFragment).commit();
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.one:
                showFragment(oneFragment);
                break;
            case R.id.two:
                showFragment(twoFragment);
                break;
            case R.id.thr:
                showFragment(thrFragment);
                break;
            case R.id.four:
                showFragment(fourFragment);
                break;
        }
        return true;
    }

    /**
     * 显示fragment
     * @param fragment
     */
    private void showFragment(Fragment fragment) {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        List<Fragment> fragments = supportFragmentManager.getFragments();
        for (Fragment f:fragments){
            fragmentTransaction.hide(f);
        }
        fragmentTransaction.show(fragment).commit();
    }
}